/**
 * 使用参考：
 *    https://blog.csdn.net/Chen_xiaobao/article/details/134569928
 *    https://developer.huawei.com/consumer/cn/codelabsPortal/carddetails/tutorials_Preferences
 * 注意事项：https://developer.huawei.com/consumer/cn/forum/topic/0202483818448710121?fid=0101587866109860105
 */
import dataPreferences from '@ohos.data.preferences'

const PREFERENCES_NAME = 'myPreferences' // 首选项名字
const TOKEN_KEY = 'TOKEN_KEY' // 首选项Key字段

export default class PreferencesUtil {
  static preferences = null

  // 创建dataPreferences
  static async createPreferences(context) {
    // 获取首选项实例
    this.preferences = await dataPreferences.getPreferences(context, PREFERENCES_NAME);
  }

  // 保存token
  static saveToken(token: string) {
    // 保存数据
    this.preferences.put(TOKEN_KEY, token)
    this.preferences.flush()
  }

  // 获取token
  static async getToken() {
    return this.preferences.get(TOKEN_KEY, "")
  }
}

